package com.stu.manager.studentscoremanagersys.config;

import com.stu.manager.studentscoremanagersys.interceptor.AuthorationInterCeptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    AuthraionConfigProperties authraionConfigProperties;

    @Bean
    public AuthorationInterCeptor  authorationInterCeptor(){
        return new AuthorationInterCeptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authorationInterCeptor()).addPathPatterns("/**")
                .excludePathPatterns(authraionConfigProperties.getIgnoreUrlList());
    }

    /**
     *
     * 注意：跨域是浏览器的安全策略，postman可以访问的，配置之后浏览器和前端就可以访问后端接口
     * 前后端分离项目，设置跨域
     * addMapping,可以访问 /** 后端所有请求
     * allowedOriginPatterns 允许前端所有请求
     * allowCredentials 允许携带token
     * allowedHeaders 允许携带请求头
     * allowedMethods 允许所有请求方式
     * maxAge 设置3600s
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedHeaders("*")
                .allowedMethods("*")
                .maxAge(3600);
    }
}
